<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" layout:decorate="~{layouts/main}">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div layout:fragment="content" style="width: 1200px">
    <div><button type="button" class="layui-btn layui-btn-lg" onclick="newGoods()">新增商品</button></div>
    <table class="layui-hide" id="list" lay-filter="good"></table>
    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="put">上架</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="offShelf">下架</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
</div>
</body>
<th:block layout:fragment="pageScripts">
    <script th:inline="javascript">
        var deleteUrl = /*[[@{/goods/delete/}]]*/;
        var showUrl = /*[[@{/goods/show/}]]*/;
        var putUrl = /*[[@{/goods/put/}]]*/;
        var offShelfUrl = /*[[@{/goods/offShelf/}]]*/;
        layui.use('table', function(){
            var table = layui.table;
            table.render({
                elem: '#list'
                ,url: /*[[@{/goods/list}]]*/''
                ,cols: [
                    [
                        {field:'id', width:300, title: 'ID'}
                        ,{field:'isCheck', width:200, title: '审核状态',toolbar:'#isCheck'}
                        ,{field:'isShelf', width:200, title: '审核状态',toolbar:'#isShelf'}
                        ,{field:'name', width:200, title: '商品名称'}
                        ,{field:'remarks', width:200, title: '商品介绍'}
                        ,{field:'houseProvince', width:200, title: '发布位置（省）'}
                        ,{field:'houseCity', width:200, title: '发布位置（市）'}
                        ,{field:'houseDistrict', width:200, title: '发布位置（区县）'}
                        ,{field:'right', width:250, align:'center', toolbar: '#bar',fixed:'right'}
                    ]
                ]
                ,page: true
            });

            //监听工具条offShelf
            table.on('tool(good)', function(obj){
                var data = obj.data;
                if(obj.event === 'put'){
                    $.ajax({
                        type: "POST",
                        url: putUrl+data.id,
                        success: function (result) {
                            if (result.state == '200'){
                                layer.alert(result.msg, {
                                    skin: 'layui-layer-molv'
                                    , closeBtn: 0
                                }, function(){window.location.reload();})
                            }
                        }
                    });
                } else if(obj.event === 'offShelf'){
                    $.ajax({
                        type: "POST",
                        url: offShelfUrl+data.id,
                        success: function (result) {
                            if (result.state == '200'){
                                layer.alert(result.msg, {
                                    skin: 'layui-layer-molv'
                                    , closeBtn: 0
                                }, function(){window.location.reload();})
                            }
                        }
                    });
                }else if(obj.event === 'del'){
                    layer.confirm('确认删除？', function(index){
                        $.ajax({
                            type: "POST",
                            url: deleteUrl+data.id,
                            success: function (result) {
                                if (result.state == '200'){
                                    obj.del();
                                }
                            }
                        });
                        layer.close(index);
                    });
                } else if(obj.event === 'edit'){
                    layer_show('编辑',showUrl+data.id,570,600)
                }
            });
        });

        function newGoods() {
            layer_show("新增商品",/*[[@{/goods/showNewGoods}]]*/,570,600)
        }
    </script>
    <script type="text/html" id="isCheck">
        {{#if (d.isCheck == 0){ }}
        <span>未审核</span>
        {{# }else if(d.isCheck == 1){ }}
        <span>已审核</span>
        {{# } }}
    </script>
    <script type="text/html" id="isShelf">
        {{#if (d.isShelf == 0){ }}
        <span>未上架</span>
        {{# }else if(d.isShelf == 1){ }}
        <span>已上架</span>
        {{# }else if(d.isShelf == 2){ }}
        <span>已下架</span>
        {{# }else if(d.isShelf == 3){ }}
        <span>被购买</span>
        {{# } }}
    </script>
</th:block>
</html>